// pages/bidPrice/bidPrice.js
import api from "../../api/api"
const dayjs = require("dayjs")
Page({
  data: {
    searchCondition:"",
    timeData:{},
    test: "",
    status: "",
    nowtime: "",
    inputval: "",
    show: false,
    type: "",
    bidPricelist: {
      bidStatus: 0,
      companyId: 0,
      name: "",
      price: 0,
      shippingOrderNumber: "",
      userId: 0
    },
    object: [],
    object1:[],
    echo: []
  },
  onChangeinput(event) {
    let that=this
    that.setData({
      searchCondition:event.detail
    })
    that.getbiglist(that.data.searchCondition)
  },
  onClick(){
    let that=this
    that.getbiglist(that.data.searchCondition)
  },
  finishTime(){
    let that=this
    that.setData({
      show: false
    })
    that.getbiglist()
  },
  changeTime(e){
    let that=this
    that.setData({
      [`timeData[${e.currentTarget.dataset.index}]`]:e.detail
     })
  },
  //回显接口
  echolist() {
    // 改变this指向
    let that = this;
    let companyid = wx.getStorageSync('companyid')
    api.post('/app/bidding/getBiddingOrder', {
        id: companyid
      })
      .then(res => {
        that.setData({
          echo: res.data,
        })
        //重新调用页面获取接口
        that.getbiglist()
      })
      .catch(err => {
        
      })
  },
  //页面获取接口
  getbiglist(e) {
    // 改变this指向
    let that = this;
    let companyid = wx.getStorageSync('companyid')
    api.post('/app/bidding/getBiddingOrderList', {
        id: companyid,
        shippingOrderNumber:e
      })
      .then(res => {
        that.setData({
          object: res.data,
        })
        let i =0;
        res.data.map(item=>{
          if (res.data[i].endTime) {
            var date1 = new Date(res.data[i].endTime.replace('-', '/').replace('-', '/'))
          }
          let date2 = new Date(); //获取当前时间
          let date3=dayjs(date1).valueOf()-dayjs(date2).valueOf()
          if (date3>0) {
            that.setData({
              [`object[${i}].endTime`]:date3
             })
          } else {
            that.setData({
              [`object[${i}].endTime`]:0
             })
          }
          i=i+1
        })
      })
      .catch(err => {
      })
  },
  //立即竞价接口
  bidpricelist() {
    // 改变this指向
    let that = this;
    api.post('/app/bidding/bid', that.data.bidPricelist)
      .then(res => {
        if (res.code == 200) {
          wx.showToast({
            icon: 'none',
            title: "出价成功",
            duration: 2000
          })
          //调用回显的接口.
          that.echolist()
        } else {
          wx.showToast({
            icon: 'none',
            title: "出价失败",
            duration: 2000
          })
        }

      })
      .catch(err => {
      })
  },

  onChangeprice(event) {
    let that = this
    if (that.data.status == "一轮待竞价") {
      that.setData({
        'bidPricelist.price': event.detail,
        'bidPricelist.bidStatus': "1"
      })
    } else if (that.data.status == "二轮待竞价") {
      that.setData({
        'bidPricelist.price': event.detail,
        'bidPricelist.bidStatus': "2"
      })
    }
  },

  diglogprice() {
    let that = this
    if (that.data.bidPricelist.price && that.data.bidPricelist.price != 0) {
      //调用一轮竞价的接口
      that.bidpricelist()
    } else {
      wx.showToast({
        icon: 'none',
        title: '出价不可为空或零',
        duration: 2000 //持续的时间
      })
    }

  },

  bidPrice(data) {
    let that = this
    that.setData({
      'bidPricelist.price': "",
      test: ""
    })
    that.setData({
      'bidPricelist.shippingOrderNumber': data.currentTarget.dataset.index,
    })
    if (data.currentTarget.dataset.msg == 0) {
      that.setData({
        status: "一轮待竞价"
      })
    } else if (data.currentTarget.dataset.msg == 1) {
      that.setData({
        status: "一轮出价完成"
      })
    } else if (data.currentTarget.dataset.msg == 2) {
      that.setData({
        status: "二轮待竞价"
      })
    }
    if (data.currentTarget.dataset.index == 1) {
      that.setData({
        show: false
      })
    } else {
      that.setData({
        show: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this
    let value = wx.getStorageSync('type')
    let companyid = wx.getStorageSync('companyid')
    let userid = wx.getStorageSync('id')
    that.setData({
      type: value,
      'bidPricelist.companyId': companyid,
      'bidPricelist.userId': userid,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this
    if (typeof that.getTabBar === 'function' && that.getTabBar()) {
      that.getTabBar().setData({
        // 根据tab的索引顺序是1
        select: 1
      })
    }
    that.echolist()
  },
  onRefresh() {
    // 自己定义刷新事件
    var that = this;
    // 自己定义刷新事件
    that.setData({
      triggered: true, // 将triggered属性设置为true，表示下拉刷新已经被触发
    })
    that.echolist()
    wx.showToast({
      title: "刷新成功"
    })
    setTimeout(function () {
      that.setData({
        triggered: false, // 将triggered属性设置为false，表示下拉刷新已完成
      })
    }, 500);
  }

})